import 'sqflite_helper.dart';

const String _communityName = 'communityName';
const String _faceScale = 'faceScale';
const String _faceSize = 'faceSize';
const String _faceUrl = 'faceUrl';
const String _imgLength = 'imgLength';
const String _imgWidth = 'imgWidth';
const String _imgHeight = 'imgHeight';
const String _quality = 'quality';
const String _staffName = 'staffName';
const String _staffNo = 'staffNo';
const String _uploadTime = 'uploadTime';
const String _glasses = 'glasses';
const String _reason = 'reason';
const String _gender = 'gender';
const String _scoreAvg = "scoreAvg";

class FaceQuality {
  String communityName;
  String uploadTime;
  String staffNo;
  String staffName;
  String quality;
  double imgLength;
  int imgWidth;
  int imgHeight;
  double faceScale;
  int faceSize;
  String faceUrl;
  String glasses;
  String reason;
  String gender;
  double scoreAvg;

  FaceQuality();

  Map<String, dynamic> toMap() {
    Map<String, dynamic> map = {
      _communityName: communityName,
      _faceScale: faceScale,
      _faceSize: faceSize,
      _faceUrl: faceUrl,
      _imgLength: imgLength,
      _imgWidth: imgWidth,
      _imgHeight: imgHeight,
      _quality: quality,
      _staffName: staffName,
      _staffNo: staffNo,
      _uploadTime: uploadTime,
      _glasses: glasses,
      _reason: reason,
      _gender: gender,
      _scoreAvg: scoreAvg,
    };
    return map;
  }

  FaceQuality.fromMap(Map<String, dynamic> map) {
    communityName = map[_communityName];
    faceScale = map[_faceScale];
    faceSize = map[_faceSize];
    faceUrl = map[_faceUrl];
    imgLength = map[_imgLength];
    imgWidth = map[_imgWidth];
    imgHeight = map[_imgHeight];
    quality = map[_quality];
    staffName = map[_staffName];
    staffNo = map[_staffNo];
    uploadTime = map[_uploadTime];
    glasses = map[_glasses];
    reason = map[_reason];
    gender = map[_gender];
    scoreAvg = map[_scoreAvg];
  }
}

class FaceQualityProvider {
  static FaceQualityProvider _instance;

  factory FaceQualityProvider() => _getInstance();

  static FaceQualityProvider _getInstance() {
    if (_instance == null) {
      _instance = FaceQualityProvider._();
    }
    return _instance;
  }

  //私有构造
  FaceQualityProvider._();

  static const String _table = 'ta_face_quality';

  String createSql = 'CREATE TABLE $_table (_id INTEGER PRIMARY KEY,'
      ' $_communityName TEXT, '
      ' $_staffName TEXT, '
      ' $_staffNo TEXT, '
      ' $_gender TEXT, '
      ' $_faceScale REAL, '
      ' $_faceSize INTEGER,'
      ' $_imgLength REAL, '
      ' $_imgWidth INTEGER, '
      ' $_imgHeight INTEGER, '
      ' $_scoreAvg REAL, '
      ' $_quality TEXT, '
      ' $_reason TEXT, '
      ' $_glasses TEXT, '
      ' $_faceUrl TEXT, '
      ' $_uploadTime TEXT)';

  String createIndexSql =
      'CREATE UNIQUE INDEX [IDX_${_table}_ID] ON "$_table"("$_staffNo");';

  insert(FaceQuality faceQuality) async {
    var _database = await SqfliteHelper().db;
    _database.insert(_table, faceQuality.toMap());
  }

  Future deleteByCommunityName(String communityName) async {
    var _database = await SqfliteHelper().db;
    return await _database.delete(
      _table,
      where: '$_communityName=?',
      whereArgs: [communityName],
    );
  }

  Future<List<FaceQuality>> queryAll() async {
    var _database = await SqfliteHelper().db;
    List<Map> maps = await _database.query(_table);
    List<FaceQuality> list = [];
    maps.forEach((element) {
      list.add(FaceQuality.fromMap(element));
    });
    return list;
  }

  ///按照小区查询数据条数
  Future<int> querySizeByCommunity(String communityName) async {
    var _database = await SqfliteHelper().db;
    List<Map> maps = await _database.query(
      _table,
      where: '$_communityName=?',
      whereArgs: [communityName],
    );
    return maps.length;
  }

  Future update(FaceQuality faceQuality) async {
    var _database = await SqfliteHelper().db;
    return await _database.update(
      _table,
      faceQuality.toMap(),
      where: '$_communityName=? and $_staffName=? ',
      whereArgs: [faceQuality.communityName, faceQuality.staffName],
    );
  }

  Future clear() async {
    var _database = await SqfliteHelper().db;
    await _database.delete(_table);
  }
}
